package edu.odu.cs.sstore.client;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import edu.odu.cs.sstore.dto.DocumentDTO;
import edu.odu.cs.sstore.service.CourseService;
import edu.odu.cs.sstore.service.DocumentService;

@Controller(value="studentController")
public class StudentController {
	
	private static final Logger logger = LoggerFactory.getLogger(StudentController.class);

	@Autowired
	private CourseService courseService;

	@Autowired
	private DocumentService documentService;
	
	@RequestMapping(value="/student")
	public ModelAndView students(Locale locale, Model model) {
		
		String username = ((UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername();
		
		// Since a faculty member could also be a student,
		// don't show documents that the faculty member uploaded
		List<DocumentDTO> docs = documentService.getSharedDocuments();
		List<DocumentDTO> filteredDocs = new ArrayList<DocumentDTO>();
		
		for(DocumentDTO doc : docs) {
			if(!doc.getCreator().getUsername().equals(username))
				filteredDocs.add(doc);
		}
		
		ModelAndView mv = new ModelAndView("student");
		mv.addObject("courses", courseService.getRegisteredCourses());
		mv.addObject("documents", filteredDocs);
		return mv;
	}

}
